package com.py.gs.businese.unionbattletest;

import java.util.List;

/**
 * 从min到(max-1)中产生不重复的随机数，范围比较大的情况不适用
 * 
 * @author cyd
 * @date 2018年4月20日
 */
public class ListRandom<E> {
	private KRandom random;
	private List<E> list;
	private int pos;
	
	public ListRandom(List<E> list) {
		this.list = list;
		this.random = new KRandom();
	}
	
	public ListRandom(List<E> list, KRandom random) {
		this.list = list;
		this.random = random;
	}
	
	public void reset() {
		pos = 0;
	}
	
	public E nextInt() {
		int index = random.nextInt(list.size()-pos) + pos;
		E result = list.get(index);
		list.set(index, list.get(pos));
		list.set(pos, result);
		pos++;
		if (pos >= list.size()) {
			pos = 0;
		}
		return result;
	}
	
}
